/**
 * Copyright (c) 2011-2021, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jfinal.generator.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.template.Engine;

/**
 * Model 生成器
 */
public class ControllerGenerator {

	protected Engine engine;
	protected String template = "/com/jfinal/generator/template/controller_template.jf";
	private String bean;
	private String path;
	// model路径
	private String model;
	// controller描述
	private String remark;
	// model 类名
	private String modelName;
	// service 所使用包名
	private String servicePackageName;
	// controller 所使用包名
	private String controllerPackageName;
	// controller 文件保存路径
	private String controllerOutputDir;

	public ControllerGenerator(String bean, String path, String model, String remark, String modelName, String controllerPackageName, String controllerOutputDir, String servicePackageName) {
		this.bean = bean;
		this.path = path;
		this.model = model;
		this.remark = remark;
		this.modelName = modelName;
		this.controllerPackageName = controllerPackageName;
		this.servicePackageName = servicePackageName;
		this.controllerOutputDir = controllerOutputDir;
		initEngine();
	}

	protected void initEngine() {
		engine = new Engine();
		engine.setToClassPathSourceFactory();
		engine.addSharedMethod(new StrKit());
	}

	public void generate() {
		try {
			writeToFile(genControllerContent());
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	protected String genControllerContent() {
		Kv data = Kv.by("model", model);
		data.set("bean", bean);
		data.set("path", path);
		data.set("remark", remark);
		data.set("modelName", modelName);
		data.set("controllerPackageName", controllerPackageName);
		data.set("servicePackageName", servicePackageName);
		String ret = engine.getTemplate(template).renderToString(data);
		return ret;
	}

	/**
	 * 若文件存在，则不生成，以免覆盖用户手写的代码
	 */
	protected void writeToFile(String controllerContent) throws IOException {
		File dir = new File(controllerOutputDir);
		if (!dir.exists()) {
			dir.mkdirs();
		}

		String target = controllerOutputDir + File.separator + StrKit.firstCharToUpperCase(bean) + "Controller.java";

		File file = new File(target);
		if (file.exists()) {
			return; // 若Conreoller 存在，不覆盖
		}

		try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) {
			osw.write(controllerContent);
		}
	}
}
